package crayon.reportapphis.db;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import crayon.reportapphis.util.GDB;


public class DataBaseManager extends OrmLiteSqliteOpenHelper{
	// name of the database file for your application -- change to something appropriate for your app
		private static final String DATABASE_NAME = "reportapphis.sqlite";
		// any time you make changes to your database objects, you may have to increase the database version
		private static final int DATABASE_VERSION = 1;

		// the DAO object we use to access the SimpleData table
		private Dao<SimpleData, Integer> simpleDao = null;
		private RuntimeExceptionDao<SimpleData, Integer> simpleRuntimeDao = null;
		// we do this so there is only one helper
		private static DataBaseManager helper = null;
		private static final AtomicInteger usageCounter = new AtomicInteger(0);
		/**
		 * Get the helper, possibly constructing it if necessary. For each call to this method, there should be 1 and only 1
		 * call to {@link #close()}.
		 */
		public static synchronized DataBaseManager getHelper(Context context) {
			if (helper == null) {
				helper = new DataBaseManager(context);
			}
			usageCounter.incrementAndGet();
			return helper;
		}
		
		public DataBaseManager(Context context) {
			super(context,DATABASE_NAME, null, DATABASE_VERSION);

		}

		/**
		 * This is called when the database is first created. Usually you should call createTable statements here to create
		 * the tables that will store your data.
		 */
		@Override
		public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
			GDB.log(">>onCreate"+db.getPath());
			
			try {
				GDB.log(DataBaseManager.class, "onCreaite");
				TableUtils.createTable(connectionSource, SimpleData.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}

			long millis = System.currentTimeMillis();
			// create some entries in the onCreate
			// here we try inserting data in the on-create as a test
			
			try {
				getSimpleDataDao().create(new SimpleData(millis));
				
			} catch (Exception e) {
				// TODO: handle exception
				GDB.log("ERR" +e.getMessage());
			}
			
			
			GDB.log(DataBaseManager.class,"created new entries in onCreate: " + millis);
		}

		/**
		 * This is called when your application is upgraded and it has a higher version number. This allows you to adjust
		 * the various data to match the new version number.
		 */
		@Override
		public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
			try {
				GDB.log(this.getClass(), "onUpgrade");
				TableUtils.dropTable(connectionSource, SimpleData.class, true);
				// after we drop the old databases, we create the new ones
				onCreate(db, connectionSource);
				
				
				
			} catch (SQLException e) {
				GDB.log(this.getClass(), "Can't drop databases", e);
				throw new RuntimeException(e);
			}

		}

		/**
		 * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached
		 * value.
		 */

		
		public Dao<SimpleData, Integer> getDao() throws SQLException {
			if (simpleDao == null) {
				simpleDao = getDao(SimpleData.class);
			}
			return simpleDao;
		}	
		/**
		 * Returns the RuntimeExceptionDao (Database Access Object) version of a Dao for our SimpleData class. It will
		 * create it or just give the cached value. RuntimeExceptionDao only through RuntimeExceptions.
		 */
		public RuntimeExceptionDao<SimpleData, Integer> getSimpleDataDao() {
			if (simpleRuntimeDao == null) {
				simpleRuntimeDao = getRuntimeExceptionDao(SimpleData.class);
			}
			return simpleRuntimeDao;
		}

		public void resetHelper(){
			
			helper = null;
		}
		/**
		 * Close the database connections and clear any cached DAOs.
		 */
		@Override
		public void close() {
			if (usageCounter.decrementAndGet() == 0) {
				super.close();

				simpleDao=null;
				helper = null;
				
			}
		}
		
		

}
